package com.googlecode.tidej.client.util;


import org.vectomatic.file.File;
import org.vectomatic.file.FileReader;
import org.vectomatic.file.events.LoadEndEvent;
import org.vectomatic.file.events.LoadEndHandler;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class FileDataSource implements DataSource {

  private File file;
  
  public FileDataSource(File file) {
    this.file = file;
  }
    
  @Override
  public void getContent(final AsyncCallback<String> callback) {
    final FileReader reader = new FileReader();
    reader.addLoadEndHandler(new LoadEndHandler() {
      @Override
      public void onLoadEnd(LoadEndEvent event) {
        String result = reader.getStringResult();
        callback.onSuccess(result);
        
      }
    });
    reader.readAsText(file);
  }

  @Override
  public void setContent(String string, final AsyncCallback<Void> callback) {
    if (callback == null) {
      throw new UnsupportedOperationException();
    } else {
      callback.onFailure(new UnsupportedOperationException());
    }
  }

}
